/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

import { CdkConnectedOverlay } from '@angular/cdk/overlay';
import {
  ChangeDetectorRef,
  EventEmitter,
  Host,
  Input,
  NgZone,
  OnChanges,
  Optional,
  Output,
  ViewChild
} from '@angular/core';
import { toBoolean, NgStyleInterface, ApesNoAnimationDirective, POSITION_MAP } from '@shared/apes/core';

import { ApesToolTipTrigger } from '../apes-tooltip.definitions';
import { ApesToolTipBaseComponent } from './apes-tooltip-base.component';

/**
 * This component overrides some properties of `ApesTooltipBaseComponent` and make them
 * input properties.
 *
 * @deprecated 9.0.0 tooltip and other components deprecate the old API. This
 * would be removed in 9.0.0.
 *
 * @example This example is what going to be removed
 *
 * ```html
 * <nz-tooltip>
 *   <a nz-tooltip></a>
 * </nz-tooltip>
 * ```
 */
export class ApesToolTipBaseComponentLegacy extends ApesToolTipBaseComponent implements OnChanges {
  @ViewChild('overlay', { static: false }) overlay: CdkConnectedOverlay;

  @Input() apesOverlayClassName = '';
  @Input() apesOverlayStyle: NgStyleInterface = {};
  @Input() apesMouseEnterDelay = 0.15; // second
  @Input() apesMouseLeaveDelay = 0.1; // second

  // TODO: placement logic should be removed into `ApesTooltipBaseDirective` once this component is removed.
  @Input()
  set apesPlacement(value: string) {
    if (value !== this._placement) {
      this._placement = value;
      this._positions = [POSITION_MAP[this.apesPlacement], ...this._positions];
    }
  }

  get apesPlacement(): string {
    return this._placement;
  }

  @Input()
  set apesVisible(value: boolean) {
    const visible = toBoolean(value);
    if (this._visible !== visible) {
      this._visible = visible;
      this.apesVisibleChange.emit(visible);
    }
  }

  get apesVisible(): boolean {
    return this._visible;
  }

  @Input()
  set apesTrigger(value: ApesToolTipTrigger) {
    this._trigger = value;
    this._hasBackdrop = this._trigger === 'click';
  }

  get apesTrigger(): ApesToolTipTrigger {
    return this._trigger;
  }

  @Output() readonly apesVisibleChange = new EventEmitter<boolean>();

  constructor(cdr: ChangeDetectorRef, @Host() @Optional() public noAnimation?: ApesNoAnimationDirective) {
    super(cdr, noAnimation,);
  }

  ngOnChanges(): void {
    Promise.resolve().then(() => {
      this.updatePosition();
    });
  }
}
